<template>
  <div class="container" v-if="!showDetail">
    <div class="header">
      <div class="title">分享列表</div>
    </div>
    <el-table stripe v-loading="loading" :data="tableData">
      <el-table-column prop="id" label="id" width="100"></el-table-column>
      <el-table-column prop="img" label="提现二维码" width="210">
        <template v-if="scope.row.qeid" slot-scope="scope">
          <el-image
            :src="scope.row.qeid"
            :preview-src-list="imgSrcList"
            style="max-height: 60px; max-width:100px;"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="用户昵称" width="150"></el-table-column>
      <el-table-column prop="balance" label="待提现总金额" width="170" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="is_need" label="是否需要提现" width="150" :show-overflow-tooltip="true">
        <template slot-scope="scopes">
          <el-tag
            type="danger"
            effect="dark"
            v-if="scopes.row.is_need==='需要'"
          >{{ scopes.row.is_need }}</el-tag>
          <el-tag
            type="success"
            effect="dark"
            v-if="scopes.row.is_need==='不需要'"
          >{{ scopes.row.is_need }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="联系方式" width="180" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column fixed="right" width="210" label="操作">
        <template slot-scope="scope">
          <el-button @click.prevent="handleDetail(scope.row)" type="primary" plain size="mini">明细</el-button>
          <el-button
            @click.prevent="handleDelete(scope.row)"
            type="danger"
            plain
            size="mini"
            v-permission="{ permission: '按id修改分销信息', type: 'disabled' }"
          >完成转账操作</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @current-change="handleCurrentChange"
        :background="true"
        :page-size="pageCount"
        :current-page="currentPage"
        v-if="refreshPagination"
        layout="prev, pager, next, jumper"
        :total="totalNums"
      ></el-pagination>
    </div>
  </div>
  <!-- 详情页 -->
  <distribution-item v-else :distributionId="detailDistributionId" @detailClose="detailClose"></distribution-item>
</template>

<script>
import Distribution from '@/model/distribution.js'
import DistributionItem from './distribution-item.vue'

export default {
  components: {
    DistributionItem,
  },
  data() {
    return {
      showDetail: false,
      tableData: [],
      imgSrcList: [], // 用于大图预览
      totalNums: 0,
      currentPage: 1,
      pageCount: 10,
      refreshPagination: true,
      detailDistributionId: 0,
      need: '需要提现',
      noNeed: '不需要提现',
    }
  },
  async created() {
    this.loading = true
    this.getDistributionList()
    this.loading = false
  },
  methods: {
    async getDistributionList() {
      const distributionList = await Distribution.getDistributionList()
      distributionList.forEach(item => {
        if (item.status === 0) {
          item.is_need = '需要'
        } else {
          item.is_need = '不需要'
        }
        item.balance = `${item.balance / 100}元`
      })
      console.log(distributionList)
      this.tableData = distributionList
      this.initImgSrcList()
    },
    // 切换table页
    async handleCurrentChange(val) {
      this.imgSrcList = []
      this.currentPage = val
      this.loading = true
      this.getDistributionList()
      this.loading = false
    },
    initImgSrcList() {
      this.tableData.forEach(item => {
        if (!item.qeid) {
          return
        }
        this.imgSrcList.push(item.qeid)
      })
    },
    handleDetail(val) {
      this.showDetail = true
      this.detailDistributionId = val.id
      // this.$router.push({ path: `/banner/${val.id}` })
    },
    handleDelete(val) {
      let res
      this.$confirm('此操作将认定人工转账完成，并修改状态，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        try {
          this.loading = true
          res = await Distribution.changeState({
            id: val.id,
            status: 1,
          })
          this.loading = false
          await this.getDistributionList()
          this.$message({
            type: 'success',
            message: `${res.message}`,
          })
        } catch (e) {
          this.loading = false
          console.log(e)
        }
      })
    },
    rowClick() {},
    detailClose() {
      this.showDetail = false
      this.getDistributionList()
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
</style>
